package com.power.sifa.correction.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import com.power.sifa.correction.model.Crc_person;

import javax.transaction.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * 社区矫正人员信息 持久层(hibernate)
 */

public interface Crc_personDao extends JpaRepository<Crc_person, Serializable>, JpaSpecificationExecutor<Crc_person>, Repository<Crc_person, Serializable>, Crc_personDaoCustom<Crc_person, Serializable> {

   /**
    * 逻辑删除
    * @param ids
    */
   @Modifying
   @Transactional
   @Query("update Crc_person s set s.deleted = 1  where  s.id in (:ids) ")
   void removeByIds(@Param("ids") List<String> ids);

   // 删除标志 1： 删除
   @Query(" from Crc_person s where s.cardno in (:cardNos) and s.deleted = 0 ")
   List<Crc_person> findByCardNos(@Param("cardNos") List<String> cardNos);

   @Query(" from Crc_person s where s.cardno = :cardNos and s.deleted = 0 ")
   Crc_person findByCardNo(@Param("cardNos") String cardNos);


   @Query(" from Crc_person s where s.id in (:personIds) and s.deleted = 0 ")
   List<Crc_person> findByPersonIds(@Param("personIds") List<String> personIds);


   /**
    * 查询有效社矫人员列表
    * @return
    */
//   @Query(" from Crc_person s where s.deleted = 0")
   @Query(" from Crc_person s where s.deleted = 0 and s.focus = 3200")
   List<Crc_person> getList();

   /**
    * 通过社矫人员的adid查询对应的部门信息
    * @param adid
    * @return
    */
//   @Query(value = "select a.*, b.departname as deptname from Crc_person a left join crc_depart b on a.deptid = b.departid where a.id = :id", nativeQuery = true)
//   Crc_person queryById(String id);


   @Query(value = "select * from v_rel_dept_correctuser where adid = :adid" , nativeQuery = true)
   List<Map<String, Object>> findDepartByAdid (@Param("adid") String adid);

   @Query(value = "select * from v_rel_dept_correctuser where adid in (:adids) " , nativeQuery = true)
   List<Map<String, Object>> findDepartByAdids (@Param("adids") List<String> adids);

   @Query(value = "select * from v_rel_dept_correctuser where didepartid in (:fids) " , nativeQuery = true)
   List<Map<String, Object>> findDepartByParentIds (@Param("fids") List<String> fids);

   /**
    * 查询重点布控人员列表
    * @param isFocus
    * @return
    */
   @Query(" from Crc_person s where s.focus = :isFocus")
   List<Crc_person> queryListByIsFocus(Integer isFocus);
}
